import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { EdgroupService } from '../../service/firmware/edgroup.service';
import { resBuild } from '@utils/resBuild';

@Controller('/edgroup')
export class EdgroupController {
  @Inject()
  edgroupService: EdgroupService;

  // 创建教育集团
  @Post('/create')
  async create(@Body() body) {
    try{
      const { name, photo } = body;
    await this.edgroupService.createEdgroup(name, photo);
    return resBuild.success("新增成功！");
    }catch(err){
      return resBuild.fail("新增失败", err.message)
    }
    
  }

  // 获取所有教育集团
  @Get('/list')
  async list(@Query() params) {
    console.log('params',params)
    const edgroups = await this.edgroupService.getAllEdgroups(params);
    console.log('edgroups', edgroups)
    return resBuild.data(edgroups);
  }

  // 获取单个教育集团
  @Get('/:id')
  async getById(@Param('id') id: number) {
    const edgroup = await this.edgroupService.getEdgroupById(id);
    if (!edgroup) return resBuild.fail("教育集团不存在");
    return resBuild.data(edgroup);
  }

  // 更新教育集团
  @Put('/update/:id')
  async update(@Param('id') id: number, @Body() body) {
    try{
      const { name, photo } = body;
      const updatedEdgroup = await this.edgroupService.updateEdgroup(id, name, photo);
      if (!updatedEdgroup) return resBuild.fail("更新失败，教育集团不存在");
      return resBuild.success("更新成功");
    }catch(err){
      return resBuild.fail("更新失败", err.message)
    }
    
  }

  // 删除教育集团
  @Del('/delete/:id')
  async delete(@Param('id') id: number) {
    console.log('id1',id)
    const deleted = await this.edgroupService.deleteEdgroup(id);
    if (!deleted) return resBuild.fail("删除失败，教育集团不存在");
    return resBuild.success("删除成功");
  }
}
